﻿using System;
using System.Collections.Generic;

using System.Text;
using OpenTK.Input;
using OpenTK;

namespace Xeno.Core
{
    public class XenoInput
    {
        private static MouseDevice Mouse = null;
        private static Dictionary<MouseButton, bool> Buttons = new Dictionary<MouseButton, bool>();
        private static Dictionary<Key, bool> Keys = new Dictionary<Key, bool>();
        /// <summary>
        /// Internal function.
        /// </summary>
        public static void Init()
        {
            Mouse = XenoApp.Instance.Mouse;
            Mouse.ButtonDown += new EventHandler<MouseButtonEventArgs>(ButtonDown);
            Mouse.ButtonUp +=new EventHandler<MouseButtonEventArgs>(Mouse_ButtonUp);
            XenoApp.Instance.Keyboard.KeyDown +=new EventHandler<KeyboardKeyEventArgs>(Keyboard_KeyDown);// new KeyDownEvent(Keyboard_KeyDown);
            XenoApp.Instance.Keyboard.KeyUp +=new EventHandler<KeyboardKeyEventArgs>(Keyboard_KeyUp);// new KeyUpEvent(Keyboard_KeyUp);
        }

        static void Keyboard_KeyUp(object obj,KeyboardKeyEventArgs e)
        {
            if (Keys.ContainsKey(e.Key))
            {
                Keys.Remove(e.Key);
            }
            Keys.Add(e.Key, false);
        }    
        
        static void Keyboard_KeyDown(object obj,KeyboardKeyEventArgs e)
        {
            if (Keys.ContainsKey(e.Key))
            {
                Keys.Remove(e.Key);
            }
            Keys.Add(e.Key, true);
        }
       

        static void Mouse_ButtonUp(object sender, MouseButtonEventArgs e)
        {
            Buttons.Remove(e.Button);
            Buttons.Add(e.Button, false);
        }

        public static void ButtonDown(object obj, MouseButtonEventArgs evt)
        {
            if (Buttons.ContainsKey(evt.Button))
            {
                Buttons.Remove(evt.Button);
            }
            Buttons.Add(evt.Button, true);
        }
        public static void ButtonUp(object obj, MouseButtonEventArgs evt)
        {
            
        }
        public static bool IsMouseDown(MouseButton button)
        {
            if (Buttons.ContainsKey(button))
            {
                return Buttons[button];
            }
            return false;
        }
        public static bool IsKeyDown(Key key)
        {
            if (!Keys.ContainsKey(key))
            {
                return false;
            }
            return Keys[key];
        }
        public static float MouseX()
        {
      
            return (float)Mouse.X;
        }
        public static float MouseY()
        {
            return (float)Mouse.Y;
        }
        public static float MouseXI()
        {
            return (float)Mouse.XDelta;
        }
        public static float MouseYI()
        {
            return (float)Mouse.YDelta;
        }
    }
}
